package com.example.puke;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;
import java.util.List;

/**
 * Created by Administrator on 2016/9/26 0026.
 */

public class MyLinearLayout extends LinearLayout {
    private static final String TAG = "MyLinearLayout";
    private int width,height,maxwidth;
    private List<Bitmap> htList;
    private int n=-1;//记录点击位置
    private static int x=0;//记录是否重复点击
    private Bitmap starpic;

    public MyLinearLayout(Context context) {
        super(context);
    }

    public MyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        maxwidth=MeasureSpec.getSize(widthMeasureSpec);
        setMeasuredDimension(maxwidth,height+height/3);
    }

    /**
     * 设置扑克宽高
     * @param width
     * @param height
     * @param htList
     */
    public void setPuKeWandH(int width, int height, List<Bitmap> htList) {
        this.width=width;
        this.height=height;
        this.htList=htList;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i=0;i<13;i++) {
            if (n == i) {//如果等于点击的条目，绘制向上，其他正常绘制。
//                Log.e(TAG,"n的值是:"+n);
                if (x % 2 != 0) {//绘制被点击的牌效果
                    //使重复点击时候能靠上和靠下效果的实现
                    if (n == 0) {//这连续的两个n==0判断是为了防止点击第一张牌时候，也会距左出现空隙
                        canvas.drawBitmap(htList.get(i),width/2*i,height/3,null);
                        canvas.drawBitmap(starpic,width/2*i,height,null);
                    }else{
                        canvas.drawBitmap(htList.get(i),width/2*i+width/6,height/3,null);
                        canvas.drawBitmap(starpic,width/2*i+width/4,height,null);
                    }
                    x++;
                }else{
                    if (n == 0) {
                        canvas.drawBitmap(htList.get(i),width/2*i,0,null);
                        canvas.drawBitmap(starpic,width/2*i,height/3*2,null);
                    }else{
                        canvas.drawBitmap(htList.get(i),width/2*i+width/6,0,null);
                        canvas.drawBitmap(starpic,width/2*i+width/4,height/3*2,null);
                    }
                    x++;
                }
            }else if (n==-1){
                //第一次默认显示的位置。
                canvas.drawBitmap(htList.get(i),width/2*i,height/3,null);
            }else{//除去第一次默认情况和被点击牌效果之外的处理都在这里面
                if (n != i) {
                    if (i==n+1) {//处理被点击后一张牌的效果
                        canvas.drawBitmap(htList.get(n+1),width/2*(n+1)+width/3,height/3,null);
                    }else{
                        if (i > n) {//处理需要散开牌后面部分的牌效果
                            canvas.drawBitmap(htList.get(i),width/2*i+width/2,height/3,null);
                        }else{//处理需要散开牌前面部分的牌效果
                            canvas.drawBitmap(htList.get(i),width/2*i,height/3,null);
                        }
                    }
                }
            }
        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x,y;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                x= (int) event.getX();
                y= (int) event.getY();
//                Log.e(TAG,"按下时："+"X坐标是："+x+" Y坐标是："+y);
                break;
            case MotionEvent.ACTION_MOVE:
                x= (int) event.getX();
                y= (int) event.getY();
                break;
            case MotionEvent.ACTION_UP:
                x= (int) event.getX();
                y= (int) event.getY();
                n=x/(width/2);//确定是第几个
                invalidate();//此时刷新如果放在最下面，会down和up都调用，所以会调用两次，会造成，x被自加两次，相当于无效。
                break;
        }
        return true;
    }
    /**
     * 绘制五角星
     * @param star
     */
    public void setStar(int star) {
        starpic= BitmapFactory.decodeResource(getResources(),star);
    }

}
